package org.yelongframework.servlet;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

/**
 * {@link HttpServletRequest}测试工具类
 * 
 * @since 1.0.0
 */
public final class HttpServletRequestTests {

	private HttpServletRequestTests() {
	}

	/**
	 * 打印 request 的请求头到控制台
	 * 
	 * @param request request
	 */
	public static void printHeader(HttpServletRequest request) {
		printHeader(request, System.out::println);
	}

	/**
	 * 打印 request 的请求头
	 * 
	 * @param request request
	 * @param printer 打印机
	 */
	public static void printHeader(HttpServletRequest request, Consumer<String> printer) {
		Enumeration<String> headerNames = request.getHeaderNames();
		printer.accept("-------------------------" + request + ".headers" + "-------------------------");
		while (headerNames.hasMoreElements()) {
			String header = headerNames.nextElement();
			printer.accept(header + "  : " + request.getHeader(header));
		}
		printer.accept("--------------------------------------------------");
	}

	/**
	 * 打印 request 的请求参数到控制台
	 * 
	 * @param request request
	 */
	public static void printParameter(HttpServletRequest request) {
		printParameter(request, System.out::println);
	}

	/**
	 * 打印 request 的请求参数
	 * 
	 * @param request request
	 * @param printer 打印机
	 */
	public static void printParameter(HttpServletRequest request, Consumer<String> printer) {
		printer.accept("-------------------------" + request + ".parameters" + "-------------------------");
		Map<String, String[]> parameterMap = request.getParameterMap();
		for (Entry<String, String[]> entry : parameterMap.entrySet()) {
			printer.accept(entry.getKey() + "  : " + Arrays.asList(entry.getValue()));
		}
		printer.accept("--------------------------------------------------");
	}

	/**
	 * 打印 request 的Cookies到控制台
	 * 
	 * @param request request
	 */
	public static void printCookies(HttpServletRequest request) {
		printCookies(request, System.out::println);
	}

	/**
	 * 打印 request 的Cookies
	 * 
	 * @param request request
	 * @param printer 打印机
	 */
	public static void printCookies(HttpServletRequest request, Consumer<String> printer) {
		printer.accept("-------------------------" + request + ".cookies" + "-------------------------");
		Cookie[] cookies = request.getCookies();
		if (null != cookies) {
			for (Cookie cookie : cookies) {
				printer.accept(cookie.getName() + " : " + cookie.getValue());
			}
		}
		printer.accept("--------------------------------------------------");
	}

	/**
	 * 打印 request 的基本信息到控制台
	 * 
	 * @param request request
	 */
	public static void printBaseInfo(HttpServletRequest request) {
		printBaseInfo(request, System.out::println);
	}

	/**
	 * 打印 request 的基本信息
	 * 
	 * @param request request
	 * @param printer 打印机
	 */
	public static void printBaseInfo(HttpServletRequest request, Consumer<String> printer) {
		printer.accept("-------------------------" + request + ".baseInfo" + "-------------------------");
		printer.accept("class : " + request.getClass());
		printer.accept("authType : " + request.getAuthType());
		printer.accept("characterEncoding : " + request.getCharacterEncoding());
		printer.accept("contentLength : " + request.getContentLength());
		printer.accept("contentLengthLong : " + request.getContentLengthLong());
		printer.accept("contentType : " + request.getContentType());
		printer.accept("contextPath : " + request.getContextPath());
		printer.accept("dispatcherType : " + request.getDispatcherType());
		printer.accept("localAddr : " + request.getLocalAddr());
		printer.accept("localName : " + request.getLocalName());
		printer.accept("localPort : " + request.getLocalPort());
		printer.accept("method : " + request.getMethod());
		printer.accept("pathInfo : " + request.getPathInfo());
		printer.accept("pathTranslated : " + request.getPathTranslated());
		printer.accept("protocol : " + request.getProtocol());
		printer.accept("queryString : " + request.getQueryString());
		printer.accept("remoteAddr : " + request.getRemoteAddr());
		printer.accept("remoteHost : " + request.getRemoteHost());
		printer.accept("remoteUser : " + request.getRemoteUser());
		printer.accept("requestedSessionId : " + request.getRequestedSessionId());
		printer.accept("requestURI : " + request.getRequestURI());
		printer.accept("requestURL : " + request.getRequestURL());
		printer.accept("scheme : " + request.getScheme());
		printer.accept("serverName : " + request.getServerName());
		printer.accept("serverPort : " + request.getServerPort());
		printer.accept("servletPath : " + request.getServletPath());
		printer.accept("--------------------------------------------------");
	}

}
